

import 'package:flutter_app_demo/models/user.dart';
import 'package:shared_preferences/shared_preferences.dart';

class DataUtils {
  static const String SP_ACCESS_TOKEN = "access_token";
  static const String SP_REFRESH_TOKEN = "refresh_token";
  static const String SP_UID = "uid";
  static const String SP_TOKEN_TYPE = "expires_in";
  static const String SP_EXPIRES_IN = "token_type";

  static const String SP_IS_LOGIN = "is_login";

  //{
  //   "access_token": "06c6902a-c4e4-443e-b6f8-0fa1292ae792",
  //   "refresh_token": "9d87add9-7d04-4bc5-8e70-104e4d29b120",
  //   "uid": 5170983,
  //   "token_type": "bearer",
  //   "expires_in": 604799
  // }
  static Future<void> saveLoginInfo(Map<String, dynamic> map) async {
    if (map != null && map.isNotEmpty) {
      SharedPreferences sp = await SharedPreferences.getInstance();
      sp
        ..setString(SP_ACCESS_TOKEN, map[SP_ACCESS_TOKEN])
        ..setString(SP_REFRESH_TOKEN, map[SP_REFRESH_TOKEN])
        ..setInt(SP_UID, map[SP_UID])
        ..setInt(SP_TOKEN_TYPE, map[SP_TOKEN_TYPE])
        ..setString(SP_EXPIRES_IN, map[SP_EXPIRES_IN])
        ..setBool(SP_IS_LOGIN, true);
    }
    //获取token
  }

  static Future<void> clearLoginInfo() async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    sp
      ..setString(SP_ACCESS_TOKEN, "")
      ..setString(SP_REFRESH_TOKEN, "")
      ..setInt(SP_UID, -1)
      ..setString(SP_TOKEN_TYPE, "")
      ..setInt(SP_EXPIRES_IN, -1)
      ..setBool(SP_IS_LOGIN, false);

    //获取token
  }

  static Future<String> getAccessToken() async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    return sp.getString(SP_ACCESS_TOKEN);
  }

  static Future<bool> isLoging() async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    return sp.getBool(SP_IS_LOGIN);
  }

  //{
  //   "gender": "female",
  //   "name": "osc_64142869",
  //   "location": "广东 深圳",
  //   "id": 5170983,
  //   "avatar": "https://www.oschina.net/img/portrait.gif",
  //   "email": "31e812d7-efa5-4a21-beb5-4aecaa0d4b35",
  //   "url": "https://my.oschina.net/u/5170983"
  // }

  static const String SP_USER_GENDER = "gender";
  static const String SP_USER_NAME = "name";
  static const String SP_USER_LOCATION = "location";
  static const String SP_USER_ID = "id";
  static const String SP_USER_AVATAR = "avatar";
  static const String SP_USER_EMAIL = "email";
  static const String SP_USER_URL = "url";

  static saveUserInfo(Map<String, dynamic> map) async {
    if (map != null && map.isNotEmpty) {
      SharedPreferences sp = await SharedPreferences.getInstance();

      String gender = map[SP_USER_GENDER];
      String name = map[SP_USER_NAME];
      String location = map[SP_USER_LOCATION];

      int id = map[SP_USER_ID];

      String avatar = map[SP_USER_AVATAR];
      String email = map[SP_USER_EMAIL];
      String url = map[SP_USER_URL];

      sp
        ..setString(SP_USER_GENDER, gender)
        ..setString(SP_USER_NAME, name)
        ..setString(SP_USER_LOCATION, location)
        ..setInt(SP_USER_ID, id)
        ..setString(SP_USER_AVATAR, avatar)
        ..setString(SP_USER_EMAIL, email)
        ..setString(SP_USER_URL, url);

      User user = new User(
        id: id,
        name: name,
        location: location,
        gender: gender,
        avatar: avatar,
        email: email,
        url: url,
      );

      return user;
    }

    return null;
  }

  static Future<User> getUserInfo() async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    bool isLogin = sp.getBool(SP_IS_LOGIN);
    if (isLogin == null || !isLogin) {
      return null;
    }

    User user = new User();
    user.name = sp.getString(SP_USER_NAME);
    user.location = sp.getString(SP_USER_GENDER);
    user.gender = sp.getString(SP_USER_NAME);

    user.avatar = sp.getString(SP_USER_AVATAR);
    user.url = sp.getString(SP_USER_URL);
    user.email = sp.getString(SP_USER_EMAIL);

    user.id = sp.getInt(SP_USER_ID);
    return user;
  }
}
